const net = require('net');

export default function startServer() {
  return new Promise((resolve) => {
    const server = net.createServer((socket: any) => {
      console.log('A client connected');

      socket.on('data', (data: any) => {
        console.log('Received data:', data.toString());
        socket.write(data); // Echo back the received data
      });

      socket.on('end', () => {
        console.log('A client disconnected');
      });
    });

    server.listen(3000, () => {
      console.log('TCP server started on port 3000');
      resolve(server); // 在服务器启动完成时解析 Promise
    });
  });
}